﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace RazakiBLL
{
    public static class MailHelper
    {
        public static bool SendSimple(string[] to, string subject, string body)
        {
            bool result = true;

            try
            {
                MailMessage mailMessage = new MailMessage();
                SmtpClient smtpClient = new SmtpClient();
                NetworkCredential networkCredential = new NetworkCredential();
                MailAddress mailAddressFrom;

                //MailMessage----

                //"name" <address>
                //mailMessage.From = new MailAddress("\"مینوسافت - روابط عمومی\" <info@minoosoft.ir>");
                //or
                mailAddressFrom = new MailAddress("info@minoosoft.ir", "مینوسافت - روابط عمومی");
                mailMessage.From = mailAddressFrom;

                foreach (var mailAddress in to)
                {
                    mailMessage.To.Add(mailAddress);
                }

                mailMessage.Subject = subject;
                mailMessage.Body = body;

                mailMessage.IsBodyHtml = true;
                mailMessage.Priority = MailPriority.Normal;

                //smtp----

                networkCredential.UserName = Encryption.Decrypt(((NetworkCredential)smtpClient.Credentials).UserName,
                                                                null, true);
                networkCredential.Password = Encryption.Decrypt(((NetworkCredential)smtpClient.Credentials).Password, null, true);

                smtpClient.Credentials = networkCredential;

                smtpClient.Send(mailMessage);
            }
            catch (Exception exc)
            {
                ExceptionManager.Log(exc);

                result = false;
            }

            return result;
        }
    }
}
